//Client

   import java.io.*;
   import java.net.*;
   import java.util.*;
   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

    public class Client implements ActionListener
   {
   	
      JFrame mainFrame;
      JPanel contents;
      JButton send;
      JTextArea output;
      JTextField input;
      BufferedReader receive;
      PrintWriter message;
		String name;
	
       public Client(boolean pri, String name)
      {
   		
			this.name = name;   	
			
         createGUI();
      	
         if(!pri)
         {
            getInfo();
         }
      	
         joinConvo();
      
      }
   
       public void createGUI()
      {
      	
      				
         mainFrame = new JFrame("Instant Messager Client");
         mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         mainFrame.setSize(500,500);
         contents = new JPanel();
      	
         GroupLayout layout = new GroupLayout(contents);			
         contents.setLayout(layout);
      	
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
      	
         output = new JTextArea();
         output.setEditable(false);
      	
         input = new JTextField();
      	
         send = new JButton("SEND");
         send.addActionListener(this);
         send.setActionCommand("send");
         send.setEnabled(false);
      				
         layout.setHorizontalGroup(
            layout.createSequentialGroup()
            .addComponent(output)
            .addComponent(input)
            .addComponent(send)
            );
      		
         layout.setVerticalGroup(
            layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(output)
            .addComponent(input)
            .addComponent(send))
            );
      		
         mainFrame.add(contents);
         mainFrame.setVisible(true);
      	
      }
   
       public void actionPerformed(ActionEvent e)
      {
      
         if(e.getActionCommand().equals("send"))
         {
         
            String temp = name + " : " + input.getText();
         	
            if(!temp.equals(""))
            {
            
               message.println(temp);
					output.append(temp + "\n");
					input.setText("");
            
            }			
         
         }
      
      }
   	
       public void getInfo()
      {
      
        	String server = JOptionPane.showInputDialog("Please type the I.P. of the infomation \n server you wish to connect to.");
			
         try
         {
            Socket inSocket = new Socket(server, 4445);
         
            BufferedReader in = new BufferedReader(new InputStreamReader(inSocket.getInputStream()));
         
            String temp = "";
         
            while(!((temp = in.readLine()).equals("")))
            {
            
               System.out.println(temp);
					            
            }
         
            inSocket.close();
         }
             catch(IOException e)
            {
            
            
            
            } 		
      	  
      }
   	
       public void joinConvo()
      {
      
         String ip = JOptionPane.showInputDialog("Which Conversation would you like to connect to? \n Insert I.P. Address");
      	
			String portIn = JOptionPane.showInputDialog("Insert Port");
         int port = Integer.parseInt(portIn);
      	
         try 
         {
         
            Socket convoSocket = new Socket(ip, port);
         	
            receive = new BufferedReader(new InputStreamReader(convoSocket.getInputStream()));
            message = new PrintWriter(convoSocket.getOutputStream(), true);
         	
            send.setEnabled(true);
         	
            boolean talking = true;
         	
            while(talking)
            {
            	
               String temp = "";
            	
               if(!((temp = receive.readLine()).equals("")))
               {
               
                  output.append(temp + "\n");
               
               }
            				
            }
         	
         }
             catch(IOException e)
            {
            }
      
      }
   	
   }